package DataDumpingManager;
use strict;
use YAML::Tiny;

sub new{
	my $config_file = shift;
	my $self = {};
	bless $self;
	return $self;
}
sub touch_dump_file{
	my $self = shift;
	my $config_file = shift;
	my $in = undef;
	open( $in,  ">>", $config_file);
	close $in;
}

sub load_dumped_data{
	my $self = shift;	
	my $dump_file = shift;
	my $data_name = shift;
	
	$self->touch_dump_file($dump_file);
	my @data_set = YAML::Tiny::LoadFile($dump_file);
	if(@data_set < 1){
		return undef;
	}
	if(exists $data_set[0]->{$data_name}){
		return $data_set[0]->{$data_name};
	}else{
		return undef;
	}	
}

sub dump_data{
	my $self = shift;
	my $dump_file = shift;
	my $data_name = shift;
	my $data_value = shift;
	$self->touch_dump_file($dump_file);
	my $yaml = YAML::Tiny->read($dump_file);
	$yaml->[0]->{$data_name} = $data_value;
	$yaml->write($dump_file);
}

sub reset_all_dumped_datas{
	my $self = shift;
	my $dump_file = shift;
	$self->touch_dump_file($dump_file);
	my $yaml = YAML::Tiny->read($dump_file);
	foreach my $key (keys %{$yaml->[0]}){
		if($yaml->[0]->{$key} =~ /\D/){
			$yaml->[0]->{$key} = '';
		}else{
			$yaml->[0]->{$key} = 0;
		}
	} 
	$yaml->write($dump_file);
}

sub reset_dumped_data{
	my $self = shift;
	my $dump_file = shift;
	my $data_name = shift;
	$self->touch_dump_file($dump_file);
	my $yaml = YAML::Tiny->read($dump_file);
	if($yaml->[0]->{$data_name} =~ /\D/){
		$yaml->[0]->{$data_name} = '';
	}else{
		$yaml->[0]->{$data_name} = 0;
	}	
	$yaml->write($dump_file); 
}

sub DESTROY{
	
}

1;
